package com.demo.service.impl;

import com.demo.common.Constants;
import com.demo.entity.UserWx;
import com.demo.service.IUserWxService;
import com.demo.vo.SalePersonVo;
import com.demo.wx.AdvancedUtil;
import com.demo.wx.Template;
import com.demo.wx.TemplateParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.dao.ISalePersonDao;
import com.demo.entity.SalePerson;
import com.demo.service.ISalePersonService;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SalePersonServiceImpl implements ISalePersonService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISalePersonDao userDao;
    @Autowired
    private IUserWxService userWxDao;

    @Override
    public List<SalePerson> queryByPage(SalePersonVo user) {
        return userDao.queryByPage(user);
    }

    @Override
    public List<SalePerson> queryByUserIdAndPage(SalePersonVo user) {
        return userDao.queryByUserIdAndPage(user);
    }

    @Override
    public SalePerson queryUserByPhone(String phone) {
        logger.info("service方法进入----");
        return null;
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SalePerson record) {
        return userDao.insert(record);
    }

    @Override
    public int insertSelective(SalePerson record) {
        return userDao.insertSelective(record);
    }

    @Override
    public Object registerByShare(SalePerson record) {

        SalePerson res = userDao.selectByOpenId(record.getOpenid());
        if (res != null) {
            return "用户已经注册!";
        }
        int newJoin = userDao.insertSelective(record);
        if (record.getUserId() != null && newJoin > 0) {
            SalePerson spShare = userDao.selectByPrimaryKey(Integer.valueOf(record.getUserId()));
            spShare.setLevel("1");
            spShare.setSalesCount(spShare.getSalesCount() + 1); //累加销售员数量
            int addSalerCount = userDao.updateByPrimaryKeySelective(spShare);
            if (addSalerCount > 0) {
                SalePerson sp = this.selectByOpenid(record.getOpenid());
                SalePerson spParent = this.selectByPrimaryKey(Integer.valueOf(record.getUserId())); // 通过unionid登录

                UserWx uwManager = new UserWx();
                uwManager.setUnionid(spParent.getOpenid());
                uwManager.setUnitid(Constants.WXUserTypeWX);
                UserWx ux1Manager = userWxDao.selectByUnionid(uwManager);
                if (ux1Manager != null && ux1Manager.getOpenid() != null) { //系统包含微信账户
                    Template tem = new Template();
                    tem.setTemplateId(Constants.tmplNewVipJoin);
                    tem.setToUser(ux1Manager.getOpenid());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
                    tem.setUrl(Constants.domain + "/wx/user/index.html");
                    List<TemplateParam> paras = new ArrayList<TemplateParam>();
                    paras.add(new TemplateParam("first", "您名下新增加销售员" + record.getUserName() + "!", "#333333"));
                    paras.add(new TemplateParam("keyword1", "HP-VIP-" + sp.getId(), "#333333"));
                    paras.add(new TemplateParam("keyword2", sdf.format(new Date()), "#333333"));
                    paras.add(new TemplateParam("remark", "点击查看!", "#333333"));
                    tem.setTemplateParamList(paras);
                    AdvancedUtil.senderTmplSms(tem);
                }
                return addSalerCount;
            } else {
                return "更新销售员数量失败!";
            }
        }
        return newJoin;
    }

    @Override
    public SalePerson selectByPhone(SalePerson user) {
        return userDao.selectByPhone(user);
    }

    @Override
    public SalePerson selectByOpenid(String openid) {
        return userDao.selectByOpenId(openid);
    }

    @Override
    public SalePerson selectByPrimaryKey(Integer id) {
        return userDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SalePerson record) {
        return userDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<SalePerson> querySaleManager(SalePersonVo sp) {
        return userDao.querySaleManager(sp);
    }

    @Override
    public int querySaleManagerCount(SalePersonVo sp) {
        return userDao.querySaleManagerCount(sp);
    }

    @Override
    public int querySaleCashCount(SalePersonVo user) {
        return userDao.querySaleCashCount(user);
    }

    @Override
    public List<SalePersonVo> querySaleCashByPage(SalePersonVo user) {
        return userDao.querySaleCashByPage(user);
    }


    @Override
    public int queryCount(SalePersonVo user) {
        return userDao.queryCount(user);
    }
}
